package com.bl._tran;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {

    public static void main(String[] args) throws Exception {
        // 1.创建事务消费者对象
        TransactionMQProducer producer = new TransactionMQProducer("tran_producer_group");
        // 2.连接mq
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 3.指定事务监听器
        producer.setTransactionListener(new MyTranListener());
        // 4.启动消费者
        producer.start();
        // 5.发送事务消息
        Message message = new Message("tran", "hello", "发送了事务消息！".getBytes(StandardCharsets.UTF_8));
        SendResult sendResult = producer.sendMessageInTransaction(message, null);
        System.out.println(sendResult);
        // 6.关闭消费者
        producer.shutdown();
    }

}